package com.ruoyi.web.controller.project;

import java.math.BigDecimal;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.project.domain.entity.ProjectMatching;
import com.ruoyi.project.service.IProjectMatchingService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;

/**
 * 配套施工Controller
 *
 * @author zzy
 * @date 2025-10-13
 */
@RestController
@RequestMapping("/project/matching")
public class ProjectMatchingController extends BaseController {
    @Autowired
    private IProjectMatchingService projectMatchingService;

    /**
     * 查询配套施工列表
     */
    @GetMapping("/list")
    public List<ProjectMatching> list(ProjectMatching projectMatching) {
       return projectMatchingService.selectProjectMatchingList(projectMatching);
    }

    /**
     * 获取总费用
     */
    @GetMapping("/getMatchingTotal")
    public BigDecimal getMatchingTotal(ProjectMatching projectMatching) {
        List<ProjectMatching> projectMatchings = projectMatchingService.selectProjectMatchingList(projectMatching);
        BigDecimal total = BigDecimal.ZERO;
        for (ProjectMatching matching : projectMatchings) {
            total = total.add(matching.getTotalPrice());
        }
        return total;
    }

    /**
     * 导出配套施工列表
     */
    @Log(title = "配套施工", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ProjectMatching projectMatching) {
        List<ProjectMatching> list = projectMatchingService.selectProjectMatchingList(projectMatching);
        ExcelUtil<ProjectMatching> util = new ExcelUtil<ProjectMatching>(ProjectMatching.class);
        util.exportExcel(response, list, "配套施工数据");
    }

    /**
     * 获取配套施工详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(projectMatchingService.selectProjectMatchingById(id));
    }

    /**
     * 新增配套施工
     */
    @Log(title = "配套施工", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ProjectMatching projectMatching) {
        return toAjax(projectMatchingService.insertProjectMatching(projectMatching));
    }

    /**
     * 修改配套施工
     */
    @Log(title = "配套施工", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ProjectMatching projectMatching) {
        return toAjax(projectMatchingService.updateProjectMatching(projectMatching));
    }

    /**
     * 删除配套施工
     */
    @Log(title = "配套施工", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(projectMatchingService.deleteProjectMatchingByIds(ids));
    }
}
